import os
import sys
import time
import threading

# Loop that read the scenario.txt file
# to play that scenario with a good timing in a thread
# made by start.py

# The loop check if there is a scenario.txt and read the scenario
# line by line, it will able the user to continue to speak to lolita : cool !
def MyTimer(tempo = 1.0):
        threading.Timer(tempo, MyTimer, [tempo]).start()
        print "right in timer for the scenario !"
	# Read play the scenario line by line and delete scenario.txt
        if os.path.exists('scenario.txt'):
		print "open scenario name txt"
		f = open("scenario.txt", "r")
		text = f.read()
		listtext = text.split('\n')
		f.close()
		os.remove('scenario.txt')
		print "open the scenario to play"
		print "text is : " + text
		f=open(listtext[0],"r")
		for line in f:
			linelist = line.split()
			print linelist
			# create me message to play the action
			command = "echo '" +  linelist[0] + "' > action.txt"
			print command
			os.system(command)
			# set the delay to wait the end of the action in second
			print "wait : " + linelist[1]
			time.sleep(float(linelist[1])+1.0)
		f.close()

# Activate timer for scenario
MyTimer(4.0)

